package org.windowkit.toolkit.widget.table;

import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.table.AbstractTableModel;

/**
 * Base class for table models.
 *
 * @author Eric Lundin
 */
public abstract class MoreAbstractTableModel extends AbstractTableModel {
	/**
	 *
	 */
	private static final long serialVersionUID = -3212269186587828860L;

	/** The number of visible rows to display */
	int visibleRows;

	/** The prefixes to display at the begining of each column's data. */
	Hashtable<Integer, String> prefixes;

    public MoreAbstractTableModel() {
		prefixes = new Hashtable<Integer, String>();
	}

	/**
	 * Get the number of visible rows to display.
	 *
	 * @return The number of rows to display.
	 */
	public int getVisibleRows() {
		return visibleRows;
	}

	/**
	 * Set the number of visible rows to display.
	 *
	 * @param visibleRows
	 *            The number of visible rows.
	 */
	public void setVisibleRows(final int visibleRows) {
		this.visibleRows = visibleRows;
		fireTableStructureChanged();
	}

	/**
	 * Set prefixes for columns. For example if a column displayed monetary
	 * values and the data was stored without a dollar sign setting this column
	 * to have a prefix of '$' would cause the dollar sign to be displayed in
	 * front of all values in the specified column.
	 *
	 * @param prefix
	 *            The prefix to use.
	 * @param columns
	 *            The columns to apply the prefix to.
	 */
	public void setPrefixForColumnValues(final String prefix, final int[] columns) {
		for (int i : columns) {
			prefixes.put(i, prefix);
		}
	}

	/**
	 * Remove the row at the given index.
	 *
	 * @param rowIndex
	 *            The index of the row to remove.
	 */
	public abstract void removeRow(int rowIndex);

	/**
	 * Gets the number of data rows this model holds.
	 *
	 * @return The number of data rows this model holds.
	 */
	public abstract int getDataRowCount();

	/**
	 * Clear the data from the table.
	 */
	public abstract void clearTableData();

	/**
	 * Clear the entire table.
	 */
	public abstract void clearTable();

	/**
	 * Get all column names.
	 *
	 * @return The names of all columns.
	 */
	public ArrayList<String> getAllColumnNames() {
        int colCount = getColumnCount();
		ArrayList<String> columnNames = new ArrayList<String>(colCount);
		for (int i = 0; i < colCount; i++) {
			columnNames.add(i, getColumnName(i));
		}
		return columnNames;
	}

	/**
	 * Retrieve the data for a given row with any necessary prefixes prepended
	 * to it.
	 *
	 * @param rowIndex
	 *            The index of the row to get the data for.
	 * @return The data for the given row with any necessary prefixes prepended
	 *         to it.
	 */
	public ArrayList<String> getPrefixedDataByRow(final int rowIndex) {
		ArrayList<String> data = new ArrayList<String>(getColumnCount());
		for (int i = 0; i < getColumnCount(); i++) {
		    Object val = getValueAt(rowIndex, i);
            data.add(i, val != null ? val.toString() : "");
		}
		return data;
	}


}
